﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace MachineBuildingSystem.OrderMgr
{
    public partial class OrderShowInfo : UserControl
    {
        private string m_CurrentUser = "";

        private string m_Password = "";

        private DataTable m_BindDT = null;
        //操作权限
        private List<long> m_ActionList = new List<long>();

        private string m_OrderCode = "";
        //选择的客户Id
        private long m_CustomerId = 0;
        //选择的物料Id
        private long m_MaterialId = 0;
        private DateTime m_Start = DateTime.Now;
        private DateTime m_End = DateTime.Now;
        //当前第几页
        private int m_CurrentPage = 1;
        //每页显示的条数
        private int m_PageSize = 50;
        //总页数
        private int m_Pages = 0;

        //记录订单Id对应的订单字典
        Dictionary<long, OrderInfo> m_OrderInfoDic = new Dictionary<long, OrderInfo>();
        //记录每页已加载的数据
        Dictionary<int, List<OrderInfo>> m_MemoryDataDic = new Dictionary<int, List<OrderInfo>>();
        //当前选择过滤的执行状态列表，默认执行和未执行

        private List<int> m_EcuteStatusList = new List<int>();
        //初始化数据是否加载完成
        private bool m_IsLoaded = false;

        public OrderShowInfo()
        {
            InitializeComponent();
        }

        private void OrderShowInfo_Load(object sender, EventArgs e)
        {
            ContextMenu emptyMenu = new ContextMenu();
            txtOrderCode.Properties.ContextMenu = emptyMenu;
            mruEditMaterial.Properties.ContextMenu = emptyMenu;
            mruEditCustomer.Properties.ContextMenu = emptyMenu;
            chkCmbExcuteStatus.Properties.ContextMenu = emptyMenu;
            mruEditCheck.Properties.ContextMenu = emptyMenu;
            dateEditStart.Properties.ContextMenu = emptyMenu;
            dateEditEnd.Properties.ContextMenu = emptyMenu;
            lblSpace.Width = Screen.GetWorkingArea(this).Size.Width / 3;
        }

        #region 公共方法
        //初始化
        public void InitOrderShowUC(string currentUser, string password, List<long> actionList,
            List<CustomerInfo> customerList, List<MaterialInfo> materialList)
        {
            try
            {
                m_CurrentUser = currentUser;
                m_Password = password;
                m_ActionList = actionList;
                m_PageSize = int.Parse(ConfigurationManager.AppSettings["PageSize"].ToString());
                int days = int.Parse(ConfigurationManager.AppSettings["DefaultTimeSpilt"].ToString());
                dateEditStart.EditValue = DateTime.Now.AddDays(days * -1).Date;
                dateEditEnd.EditValue = DateTime.Now.Date;

                InitDT();
                InitMruEditCustomer(customerList);
                InitMruEditMaterial(materialList);
                InitChkCmbExcuteStatus();
                InitMruEditCheck();
                m_IsLoaded = true;
            }
            catch (Exception ex)
            {
                LogHelper.error("界面加载异常：" + ex.Message);
                MessageBox.Show(this, "界面加载异常：" + ex.Message);
            }
        }

        //刷新订单列表
        public void RefreshOrder()
        {
            btnSearch_Click(null, null);
        }

        public void SetProgress(bool isProgress)
        {
            this.controlContainerProgress.Visible = isProgress;
            this.marqueeProgressBar.Visible = isProgress;
        }
        #endregion

        #region 初始化
        private void InitDT()
        {
            m_BindDT = new DataTable();
            m_BindDT.Columns.Add("IsHaveNewFormular");
            m_BindDT.Columns.Add("mainId");
            m_BindDT.Columns.Add("id");
            m_BindDT.Columns.Add("orderCode");
            m_BindDT.Columns.Add("orderDate", typeof(DateTime));
            m_BindDT.Columns.Add("planDate", typeof(DateTime));
            m_BindDT.Columns.Add("corpName");
            m_BindDT.Columns.Add("customerId");
            m_BindDT.Columns.Add("customerCode");
            m_BindDT.Columns.Add("customerName");
            m_BindDT.Columns.Add("materialId");
            m_BindDT.Columns.Add("materialCode");
            m_BindDT.Columns.Add("materialName");
            m_BindDT.Columns.Add("materialSstd");
            m_BindDT.Columns.Add("strengthGrade");
            m_BindDT.Columns.Add("projectName");
            m_BindDT.Columns.Add("constructSite");
            m_BindDT.Columns.Add("orderQuantity");
            m_BindDT.Columns.Add("sendQuantity");
            m_BindDT.Columns.Add("executeStatus");
            m_BindDT.Columns.Add("audited");
            m_BindDT.Columns.Add("version");
        }

        //初始化客户下拉框
        private void InitMruEditCustomer(List<CustomerInfo> customerList)
        {
            try
            {
                if (customerList == null || customerList.Count == 0)
                    return;
                mruEditCustomer.Properties.Items.Clear();
                foreach (CustomerInfo customer in customerList)
                {
                    DataInfo data = new DataInfo();
                    data.Id = customer.customerId;
                    data.Value = customer.customerName;
                    mruEditCustomer.Properties.Items.Add(data);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化客户下拉框数据异常：" + ex.Message);
            }
        }

        //初始化物料下拉框
        private void InitMruEditMaterial(List<MaterialInfo> materialList)
        {
            try
            {
                if (materialList == null || materialList.Count == 0)
                    return;
                mruEditMaterial.Properties.Items.Clear();
                foreach (MaterialInfo material in materialList)
                {
                    DataInfo data = new DataInfo();
                    data.Id = material.materialId;
                    data.Value = material.materialSstd == "" ? material.materialName
                        : (material.materialName + "(" + material.materialSstd + ")");
                    mruEditMaterial.Properties.Items.Add(data);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化客户下拉框数据异常：" + ex.Message);
            }
        }

        private void InitChkCmbExcuteStatus()
        {
            try
            {
                chkCmbExcuteStatus.Properties.Items.Clear();
                chkCmbExcuteStatus.Properties.Items.Add(
                    CommonHelper.CreateDataInfo((int)ExecuteStatusEnum.CLOSE, "关闭"), false);
                chkCmbExcuteStatus.Properties.Items.Add(
                   CommonHelper.CreateDataInfo((int)ExecuteStatusEnum.PAUSE, "作废"), false);
                chkCmbExcuteStatus.Properties.Items.Add(
                    CommonHelper.CreateDataInfo((int)ExecuteStatusEnum.EXECUTE, "执行"),true);
                chkCmbExcuteStatus.Properties.Items.Add(
                    CommonHelper.CreateDataInfo((int)ExecuteStatusEnum.UN_EXECUTE, "未执行"),true);

                m_EcuteStatusList.Add((int)ExecuteStatusEnum.EXECUTE);
                m_EcuteStatusList.Add((int)ExecuteStatusEnum.UN_EXECUTE);
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化执行状态下拉框数据异常：" + ex.Message);
            }
        }

        private void InitMruEditCheck()
        {
            try
            {
                mruEditCheck.Properties.Items.Clear();
                mruEditCheck.Properties.Items.Add("未审核");
                mruEditCheck.Properties.Items.Add("已审核");
                mruEditCheck.Properties.Items.Add("全部");
                mruEditCheck.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化审核状态下拉框数据异常：" + ex.Message);
            }
        }
        #endregion

        #region 分页
        //页数变化时操作
        private void txtBoxPageNum_TextChanged(object sender, EventArgs e)
        {
            m_BindDT.Rows.Clear();
            gdcOrder.DataSource = m_BindDT;
            if (string.IsNullOrEmpty(this.txtBoxPageNum.Text.Trim()))
            {
                lblPageInfo.Text = "       ";
                return;
            }
            if (!int.TryParse(this.txtBoxPageNum.Text.Trim(), out m_CurrentPage))
                return;
            if (m_CurrentPage > m_Pages)
                return;
            this.lblPageInfo.Text = "第" + m_CurrentPage + "页/共" + m_Pages + "页";

            this.lblFirst.Enabled = m_CurrentPage <= 1 ? false : true;
            this.lblLast.Enabled = m_CurrentPage <= 1 ? false : true;
            this.lblNext.Enabled = m_CurrentPage == 0 ? false : (m_CurrentPage == m_Pages ? false : true);
            this.lblEnd.Enabled = m_CurrentPage == 0 ? false : (m_CurrentPage == m_Pages ? false : true);

            if (!backgroundWorker.IsBusy)
            {
                this.controlContainerProgress.Visible = true;
                this.marqueeProgressBar.Visible = true;
                backgroundWorker.RunWorkerAsync();
            }
        }

        //绑定列表
        private void BindData(List<OrderInfo> orderList)
        {
            try
            {
                foreach (OrderInfo order in orderList)
                    m_BindDT.Rows.Add(order.isHaveNewFormula, order.mainId, order.id, order.orderCode, order.orderDate, order.productDate,
                        order.corpName, order.customerId, order.customerCode, order.customerName,
                        order.materialId, order.materialCode, order.materialName, order.materialSstd, order.strengthGrade, order.projectName,
                        order.constructSite, order.orderQuantity, order.sendQuantity,
                        order.executeStatus == (int)ExecuteStatusEnum.UN_EXECUTE ? "未执行" :
                        (order.executeStatus == (int)ExecuteStatusEnum.EXECUTE ? "执行" :
                        (order.executeStatus == (int)ExecuteStatusEnum.PAUSE ? "作废" : "关闭")),
                        order.audited == true ? "已审核" : "未审核", order.version);
                gdcOrder.DataSource = m_BindDT;
            }
            catch (Exception ex)
            {
                LogHelper.error("绑定订单列表异常：" + ex.ToString());
            }
        }

        //首页
        private void lblFirst_Click(object sender, EventArgs e)
        {
            txtBoxPageNum.Text = "1";
        }

        //上一页
        private void lblLast_Click(object sender, EventArgs e)
        {
            txtBoxPageNum.Text = (m_CurrentPage - 1).ToString();
        }

        //下一页
        private void lblNext_Click(object sender, EventArgs e)
        {
            txtBoxPageNum.Text = (m_CurrentPage + 1).ToString();
        }

        //尾页
        private void lblEnd_Click(object sender, EventArgs e)
        {
            txtBoxPageNum.Text = m_Pages.ToString();
        }

        //设置每页显示页数
        private void lblPageSet_Click(object sender, EventArgs e)
        {
            if (!m_ActionList.Contains((int)ActionEnum.PageSet))
            {
                MessageBox.Show(this, "该用户无设置分页个数权限！");
                return;
            }
            PageSizeSetForm form = new PageSizeSetForm();
            if (DialogResult.OK == form.ShowDialog())
            {
                if (form.PageSize.Equals(m_PageSize))
                    return;
                m_PageSize = form.PageSize;
                //如果每页显示的数量改变需重新计算页数和查询
                m_MemoryDataDic.Clear();
                m_Pages = GetPages();
                if (m_Pages == -1)
                {
                    MessageBox.Show("查询异常，详细信息请参考日志文件！");
                    return;
                }
                this.txtBoxPageNum.Text = "";
                this.txtBoxPageNum.Text = m_CurrentPage.ToString();
            }
        }

        private void SetPageStatus(bool isEnable)
        {
            this.lblFirst.Enabled = isEnable;
            this.lblLast.Enabled = isEnable;
            this.lblNext.Enabled = isEnable;
            this.lblNext.Enabled = isEnable;
        }
        #endregion

        #region 按钮事件
        //查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            //每次查询都将缓存记录的数据情况
            m_MemoryDataDic.Clear();
            m_OrderCode = this.txtOrderCode.Text.Trim();
            m_Start = Convert.ToDateTime(this.dateEditStart.EditValue);
            m_End = Convert.ToDateTime(this.dateEditEnd.EditValue);
            m_Pages = GetPages();
            this.txtBoxPageNum.Text = "";
            if (m_Pages == -1)
                return;
            this.txtBoxPageNum.Text = "1";
        }

        //获取总页数
        private int GetPages()
        {
            int pages = 0;
            try
            {
                string types = "";
                //if (m_CurrentExcuteStatus == -1)
                //{
                //    types = ((int)ExecuteStatusEnum.EXECUTE).ToString() + ":" + ((int)ExecuteStatusEnum.UN_EXECUTE).ToString()
                //        + ":" + ((int)ExecuteStatusEnum.PAUSE).ToString() + ":" + ((int)ExecuteStatusEnum.CLOSE).ToString();
                //}
                //else
                //{
                //    types = m_CurrentExcuteStatus.ToString();
                //}
                string audites = mruEditCheck.Text == "全部" ? "0:1" : (mruEditCheck.Text == "已审核" ? "1" : "0");
                int belongcorpId = int.Parse(ConfigurationManager.AppSettings["BelongCorpId"]);
                //获取该次查询后订单总数
                int saleOrderSizes = DataHelper.GetSaleOrdersSizes(m_CurrentUser, m_Password, belongcorpId,
                    m_OrderCode, m_CustomerId, 0, m_Start, m_End, types, audites);
                if (saleOrderSizes == -1)
                    return -1;
                if (saleOrderSizes == 0)
                    return 0;
                pages = saleOrderSizes % m_PageSize == 0 ? saleOrderSizes / m_PageSize
                    : saleOrderSizes / m_PageSize + 1;
            }
            catch (Exception ex)
            {
                LogHelper.error("获取总页数异常：" + ex.ToString());
                MessageBox.Show(ex.Message);
                return -1;
            }
            return pages;
        }

        //创建订单
        private void btnOrderAdd_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.OrderAdd))
            {
                MessageBox.Show(this, "该用户无创建订单权限！");
                return;
            }
            if (OrderEvent != null)
            {
                //测试用
                OrderArgument argument = new OrderArgument(OrderTypeEnum.OrderAdd, null);
                OrderEvent(this, argument);
            }
        }

        //订单详情
        private void btnOrderDetail_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            long orderId = long.Parse(currentRow["id"].ToString());
            OrderInfo order = null;
            if (m_OrderInfoDic.ContainsKey(orderId))
                order = m_OrderInfoDic[orderId];
            if (OrderEvent != null)
            {
                //测试用
                //order.id = 406;
                OrderArgument argument = new OrderArgument(OrderTypeEnum.OrderDetail, order);
                OrderEvent(this, argument);
            }
        }

        //双击订单查看详情
        private void gdcOrder_DoubleClick(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
                return;
            btnOrderDetail_Click(null, null);
        }

        //修改订单
        private void btnModifyOrder_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.OrderSearch))
            {
                MessageBox.Show(this, "该用户无修改订单权限！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            //已经审核的订单不能修改,审核过的订单可以修改预定生产时间
            bool isAudited = currentRow["audited"].ToString() == "已审核" ? true : false;
            if (isAudited)
            {
                if (DialogResult.Yes != MessageBox.Show(this, "该订单已审核,只能修改预定生产时间和实际距离,是否继续?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    return;
            }
            long orderId = long.Parse(currentRow["id"].ToString());
            OrderInfo order = null;
            if (m_OrderInfoDic.ContainsKey(orderId))
                order = m_OrderInfoDic[orderId];
            if (OrderEvent != null)
            {
                if (!isAudited)
                {
                    OrderArgument argument = new OrderArgument(OrderTypeEnum.OrderUpdate, order);
                    OrderEvent(this, argument);
                }
                else
                {
                    OrderArgument argument = new OrderArgument(OrderTypeEnum.OrderUpdateByAudited, order);
                    OrderEvent(this, argument);
                }
            }
        }

        //订单作废
        private void btnOrderCancel_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.OrderPause))
            {
                MessageBox.Show(this, "该用户无订单作废权限！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            bool isAudited = currentRow["audited"].ToString() == "已审核" ? true : false;
            if (isAudited)
            {
                MessageBox.Show(this, "订单已审核，不能作废!");
                return;
            }
            if (MessageBox.Show(this, "是否确认作废该订单？", "询问", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            long orderId = long.Parse(currentRow["id"].ToString());

            try
            {
                bool isSuccess = DataHelper.UpdateExecuteStatus(m_CurrentUser, m_Password, orderId, (int)ExecuteStatusEnum.PAUSE);
                if (!isSuccess)
                    return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "订单作废失败：" + ex.Message);
                return;
            }
            MessageBox.Show(this, "订单作废成功");
            btnSearch_Click(null, null);
        }

        //关闭订单
        private void btnClose_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.OrderClose))
            {
                MessageBox.Show(this, "该用户无关闭订单权限！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            if (MessageBox.Show(this, "是否确认关闭该订单？", "询问", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            long orderId = long.Parse(currentRow["id"].ToString());

            try
            {
                bool isSuccess = DataHelper.UpdateExecuteStatus(m_CurrentUser, m_Password, orderId, (int)ExecuteStatusEnum.CLOSE);
                if (!isSuccess)
                    return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "订单关闭失败：" + ex.Message);
                return;
            }
            MessageBox.Show(this, "订单关闭成功!");
            btnSearch_Click(null, null);
        }

        //删除订单
        private void btnOrderDelete_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.OrderDel))
            {
                MessageBox.Show(this, "该用户无删除订单权限！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            if (MessageBox.Show(this, "是否确认删除该订单？", "询问", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            long orderId = long.Parse(currentRow["id"].ToString());
            try
            {
                bool isSuccess = DataHelper.DelSaleLine(m_CurrentUser, m_Password, orderId);
                if (!isSuccess)
                    return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "删除订单失败:" + ex.Message);
                return;
            }
            MessageBox.Show(this, "删除订单成功");
            //刷新订单列表
            btnSearch_Click(null, null);
        }

        //发货单新增
        private void btnProductOrderAdd_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (!m_ActionList.Contains((int)ActionEnum.DeliAdd))
            {
                MessageBox.Show(this, "该用户无创建发货单权限！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show(this, "请先选择数据!");
                return;
            }
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            bool isExcute = currentRow["executeStatus"].ToString() == "执行" ? true : false;
            if (!isExcute)
            {
                MessageBox.Show(this, "非执行状态下订单不能发货！");
                return;
            }
            long orderId = long.Parse(currentRow["id"].ToString());
            OrderInfo order = null;
            if (m_OrderInfoDic.ContainsKey(orderId))
                order = m_OrderInfoDic[orderId];
            if (OrderEvent != null)
            {
                //测试用
                //order.id = 2001490681965;
                OrderArgument argument = new OrderArgument(OrderTypeEnum.DeliveryAdd, order);
                OrderEvent(this, argument);
            }
        }

        //发货单详情
        private void btnProductOrderDetail_Click(object sender, EventArgs e)
        {
            if (!m_IsLoaded)
            {
                MessageBox.Show(this, "正在加载数据，请稍后！");
                return;
            }
            if (gdvOrder.SelectedRowsCount == 0)
            {
                MessageBox.Show("请先选择数据!");
                return;
            }
            DataRow currentRow = gdvOrder.GetFocusedDataRow();
            long orderId = long.Parse(currentRow["id"].ToString());
            OrderInfo order = null;
            if (m_OrderInfoDic.ContainsKey(orderId))
                order = m_OrderInfoDic[orderId];
            if (OrderEvent != null)
            {
                //测试用
                //order.id = 2001490681965;
                OrderArgument argument = new OrderArgument(OrderTypeEnum.DeliveryDetail, order);
                OrderEvent(this, argument);
            }
        }
        #endregion

        #region 下拉框事件
        //客户
        private void mruEditCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mruEditCustomer.SelectedItem == null
                || mruEditCustomer.SelectedItem.ToString() == ""
                || !(mruEditCustomer.SelectedItem is DataInfo))
                m_CustomerId = 0;
            else
                m_CustomerId = ((DataInfo)mruEditCustomer.SelectedItem).Id;
        }

        //物料
        private void mruEditMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mruEditMaterial.SelectedItem == null
                || mruEditMaterial.SelectedItem.ToString() == ""
                || !(mruEditMaterial.SelectedItem is DataInfo))
                m_MaterialId = 0;
            else
                m_MaterialId = ((DataInfo)mruEditMaterial.SelectedItem).Id;
        }
        #endregion

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List<OrderInfo> orderList = null;
            try
            {
                if (m_MemoryDataDic.ContainsKey(m_CurrentPage))
                    orderList = m_MemoryDataDic[m_CurrentPage];
                else
                {
                    string types = "";
                    foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in this.chkCmbExcuteStatus.Properties.Items)
                    {
                        if (item.CheckState == CheckState.Checked)
                        {
                            DataInfo data = item.Value as DataInfo;
                            types += data.Id.ToString() + ":";
                        }
                    }
                    string audites = mruEditCheck.Text == "全部" ? "0:1" : (mruEditCheck.Text == "已审核" ? "1" : "0");
                    int belongcorpId = int.Parse(ConfigurationManager.AppSettings["BelongCorpId"]);
                    orderList = DataHelper.GetSaleOrders(m_CurrentUser, m_Password, belongcorpId, 0, m_OrderCode, m_CustomerId, m_MaterialId,
                        m_Start, m_End, types.TrimEnd(':'), audites, m_CurrentPage - 1, m_PageSize);
                }
            }
            catch
            {
                throw;
            }
            e.Result = orderList;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.controlContainerProgress.Visible = false;
            this.marqueeProgressBar.Visible = false;
            if (e.Cancelled)
                return;
            if (e.Error != null)
            {
                MessageBox.Show(this, e.Error.Message);
                return;
            }
            List<OrderInfo> orderList = e.Result as List<OrderInfo>;
            if (orderList == null || orderList.Count == 0)
                return;


            if (!m_MemoryDataDic.ContainsKey(m_CurrentPage))
                m_MemoryDataDic.Add(m_CurrentPage, orderList);
            m_OrderInfoDic.Clear();
            //按ID进行排序
            var query = from temp in orderList
                        orderby temp.orderDate descending
                        select temp;
            List<OrderInfo> tempList = query.Take<OrderInfo>(orderList.Count).ToList();
            foreach (OrderInfo order in tempList)
            {
                if (!m_OrderInfoDic.ContainsKey(order.id))
                    m_OrderInfoDic.Add(order.id, order);
            }
            try
            {
                BindData(tempList);
            }
            catch (Exception ex)
            {
                LogHelper.error("绑定订单列表异常：" + ex.ToString());
            }
        }


        public delegate void OrderEventHandler(object sender, OrderArgument e);
        public event OrderEventHandler OrderEvent;

        private void gdvOrder_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (gdvOrder.GetRow(e.RowHandle) == null)
                return;
            //获取所在行指定列的值
            bool isHaveNewFormular = Convert.ToBoolean(gdvOrder.GetRowCellValue(e.RowHandle, gdcIsHaveNewFormular));
            if (isHaveNewFormular)
            {
                e.Appearance.BackColor = Color.Pink;//设置此行的背景颜色
            }
        }

        private void gdvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}
